package com.phb.puhuibao.service.impl;

import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.idp.pub.dao.IBaseDao;
import com.idp.pub.dao.IPagerDao;
import com.idp.pub.service.impl.DefaultBaseService;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.UserAccount;
import com.phb.puhuibao.entity.UserAccountLog;
import com.phb.puhuibao.entity.ZhifubaoTransfer;
import com.phb.puhuibao.service.ZhifubaoTransferService;
 
 

@Transactional
@Service("zhifubaoTransferService")
public class ZhifubaoTransferServiceImpl extends DefaultBaseService<ZhifubaoTransfer, String> implements ZhifubaoTransferService {
	@Resource(name = "zhifubaoTransferDao")
	public void setBaseDao(IBaseDao<ZhifubaoTransfer, String> baseDao) {
		super.setBaseDao(baseDao);
	}
	@Resource(name = "zhifubaoTransferDao")
	public void setPagerDao(IPagerDao<ZhifubaoTransfer> pagerDao) {
		super.setPagerDao(pagerDao);
	}
	
	@javax.annotation.Resource(name = "mobileUserDao")
	private IBaseDao<MobileUser, String> mobileUserDao;
	
	@javax.annotation.Resource(name = "userAccountLogDao")
	private IBaseDao<UserAccountLog, String> userAccountLogDao;
	
	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	
	
	@Override
	public  void processAddNewApply(ZhifubaoTransfer entity,MobileUser user,double amount) {
 
		
		this.getBaseDao().save(entity);
		 
		MobileUser updateuser = new MobileUser();
		updateuser.setUserId(user.getUserId());
		updateuser.setFrozenMoney(user.getFrozenMoney() + amount);  // 鎶婃彁鐜伴鏀惧埌鍐荤粨棰濆害閲�
		mobileUserDao.update(updateuser);
		
		 MobileUser userquery = mobileUserDao.get(user.getUserId()+"");
		 
		UserAccountLog log = new UserAccountLog();
		log.setUserId(entity.getMuserId());
		log.setAmount(  entity.getAmount());
		log.setBalanceAmount(userquery.getUserMoney() - userquery.getFrozenMoney() );  
		log.setChangeType("鎻愮幇鍐荤粨");
		log.setChangeDesc(entity.getAliAccount());
		log.setAccountType(0);
		userAccountLogDao.save(log);
 
	}
	@Override
	public void processFinnishTransfer(ZhifubaoTransfer fer,String name,int audituserid) {
		
		
		MobileUser  user =  mobileUserDao.get(fer.getMuserId()+"");
		MobileUser updateuser = new MobileUser();
		updateuser.setUserId(user.getUserId());
		updateuser.setUserMoney(user.getUserMoney() - fer.getAmount());
		updateuser.setFrozenMoney(user.getFrozenMoney() - fer.getAmount());
		mobileUserDao.update(updateuser);
		
 
		
		UserAccountLog log = new UserAccountLog();
		log.setUserId(user.getUserId());
		log.setAmount(- fer.getAmount());
		log.setBalanceAmount(user.getUserMoney()  - user.getFrozenMoney()  ); // 鎻愮幇鍐荤粨浣欓涓嶅彉 鎻愮幇瀹屾垚,浣欓鍑忓皯
		log.setChangeType("鏀粯瀹濇彁鐜板畬鎴�");
		log.setChangeDesc(fer.getAliAccount());
		log.setAccountType(0);
		userAccountLogDao.save(log);
		
		ZhifubaoTransfer updatefer = new ZhifubaoTransfer();
		updatefer.setAuditUsername(name);
		updatefer.setAuditUser(audituserid);
		updatefer.setIsTransfered(1);
		updatefer.setRecordId(fer.getRecordId());
		this.getBaseDao().update(updatefer);
		
 
		
	}
	
 

}
